import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:bike_ride/generated/l10n.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:url_launcher/url_launcher.dart';

/// 拨打电话
class PhoneCallUtil {
  static Future showPhoneCallModalPopup(
      BuildContext context, String? phone) async {
    if (phone != null && phone.isNotEmpty) {
      final textStyle = TextStyle(
        fontSize: 18,
        fontWeight: FontWeight.w500,
        height: 1.3,
        color: Theme.of(context).colorScheme.primary,
      );

      Widget actionSheet = CupertinoActionSheet(
        title: null,
        actions: <Widget>[
          CupertinoActionSheetAction(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Icon(
                  Icons.phone,
                  size: 24,
                  color: Colors.black,
                ),
                SizedBox(
                  width: 30.rpx,
                ),
                Text(
                  '${S.current.call} $phone',
                  style: textStyle,
                ),
              ],
            ),
            onPressed: () async {
              // 关闭菜单
              Navigator.of(context).pop();

              String url = phone;
              final Uri launchUri = Uri(scheme: 'tel', path: url);
              bool canTel = await canLaunchUrl(launchUri);

              if (canTel) {
                await launchUrl(launchUri);
              } else {
                EasyLoading.showToast(S.current.callFail);
              }
            },
          ),
        ],
        cancelButton: CupertinoActionSheetAction(
          child: Text(
            S.current.cancel,
            style: TextStyle(
              fontSize: 36.rpx,
              fontWeight: FontWeight.w500,
              color: Theme.of(context).colorScheme.primary,
            ),
          ),
          onPressed: () {
            // 关闭菜单
            Navigator.of(context).pop();
          },
        ),
      );

      return showCupertinoModalPopup(
        context: context,
        builder: (BuildContext context) => actionSheet,
      );
    } else {
      EasyLoading.showError(S.current.phoneEmpty);
    }
  }
}
